﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ShareContent;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace AncientEmpires_025_132
{
    public class PlayingMenu : Menu
    {
        // Event for select main menu
        public delegate void EventHandleMenuSelect(int choice);
        public event EventHandleMenuSelect MenuSelect;

        PlayingMenuWithXML playingMenuWithXML;
        Sprite2D border;
        List<MyButton> listButton;

        public PlayingMenu(ContentManager content, string strmenuxml)
        {
            playingMenuWithXML = content.Load<PlayingMenuWithXML>(strmenuxml);
            SizeScale = playingMenuWithXML.sizeScale;
            IsActive = false;
            InitMenu(content);
        }

        public void InitMenu(ContentManager content)
        {
            Background = LoadStaticItem(content, playingMenuWithXML.strBackground, playingMenuWithXML.sizeScale, new Vector2(0,0), "", new Vector2(0, 0));

            border = LoadStaticItem(content, playingMenuWithXML.strBorder, playingMenuWithXML.strSizeScaleBorder, playingMenuWithXML.strPositionScaleBorder, "", Background.Position);

            listButton = LoadListItem(content, playingMenuWithXML.listButton, "", Background.Position);
            for (int i = 0; i < listButton.Count; i++ )
            {
                listButton[i].ClickButtonWithSender += new MyButton.EventHandleClickWithSender(PlayingMenuSelect);
            }
        }

        public void PlayingMenuSelect(object sender)
        {
            int choice = listButton.IndexOf((MyButton)sender);
            if (choice != -1) MenuSelect(choice);
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            if (IsActive)
            {
                Vector2 SizeWindow = new Vector2(spriteBatch.GraphicsDevice.Viewport.Bounds.Width,
                                             spriteBatch.GraphicsDevice.Viewport.Bounds.Height);
                Size = SizeWindow;

                Background.Size = Size;

                border.Size = new Vector2(playingMenuWithXML.strSizeScaleBorder.X * Size.X, playingMenuWithXML.strSizeScaleBorder.Y * Size.Y);
                border.Position = new Vector2(playingMenuWithXML.strPositionScaleBorder.X * Size.X, playingMenuWithXML.strPositionScaleBorder.Y * Size.Y);


                Background.Draw(gameTime, spriteBatch);

                border.Draw(gameTime, spriteBatch);

                for (int i = 0; i < listButton.Count; i++)
                {
                    listButton[i].SetPosition(new Vector2(playingMenuWithXML.listButton[i].positionScale.X * Size.X, playingMenuWithXML.listButton[i].positionScale.Y * Size.Y));
                    listButton[i].SetSize(new Vector2(playingMenuWithXML.listButton[i].sizeScale.X * Size.X, playingMenuWithXML.listButton[i].sizeScale.Y * Size.Y));
                    listButton[i].Draw(gameTime, spriteBatch);
                }
            }
        }

        public override void Update(GameTime gametime)
        {
            if (IsActive)
            {
                Background.Update(gametime);

                border.Update(gametime);

                for (int i = 0; i < listButton.Count; i++)
                {
                    listButton[i].Update(gametime);
                }
            }
        }
    }
}
